const express = require('express');
const router = express.Router();
const db = require('../../../../util/dbConfig/dbConfig')
const dateReplace = require('../../../../public/javascripts/timeFormat')

//修改
router.post('/' , async (req,res) => {
    // let obj = {
    //     id:1,     //销售订单
    //     //上面为索引条件
    //     //下面为修改内容
    //     ramPrice:1,      //公羊单价
    //     ewePrice:1,      //母羊单价
    //     totalPrice:1,    //总价
    //     phone:1,         //买方联系方式
    //     remark:'后端不小心修改了',        //备注
    // }
    let {
        id,     //销售订单
        //上面为索引条件
        //下面为修改内容
        ramPrice,      //公羊单价
        ewePrice,      //母羊单价
        totalPrice,    //总价
        phone,         //买方联系方式
        remark,        //备注
    } = req.body
    if(!id){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    let dataArr = [
        ramPrice,      //公羊单价
        ewePrice,      //母羊单价
        totalPrice,    //总价
        phone,         //买方联系方式
        remark,        //备注
    ]
    let decArr = [
        'RAM_PRICE',
        'SHEEP_PRICE',
        'TOTAL_PRINCES',
        'BUYER_PHONENUM',
        'REMARK'
    ]
    let dataArrA = []
    let decArrA = []
    for(let i = 0; i<dataArr.length; i++){
        if(dataArr[i]){
            dataArrA.push(dataArr[i])
            decArrA.push(decArr[i])
        }
    }
    if(dataArrA.length === 0){
        return res.send({
            code: 1,
            msg: '您无修改内容'
        })
    }
    try{
        let sql = `update market set`
        //拼接sql语句
        for(let j = 0; j< dataArrA.length; j++){
            if(dataArrA[j]){
                sql = sql + ' ' + `${decArrA[j]}='${dataArrA[j]}' `
            }
            if(j !== dataArrA.length -1){
                sql = sql + ','
            }
        }
        sql = sql + ' ' + `where ID=${id}`
        let result = await db.SySqlConnect(sql,[])
        if(result.affectedRows !== 0 ){
            return res.send({
                code: 0,
                msg: '修改成功'
            })
        }else {
            return res.send({
                code: 2,
                msg: '修改失败'
            })
        }
    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})

module.exports = router
